//
//  SEChatAPI.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/27.
//

import Moya

enum SEChatAPI {
    case sendGuideInquirySuccessMessage(id: String)
    case listOfChatRecords(relationId: String, pageNum: Int, pageSize: Int)
    case generateMessageId(relationId: String, msgType: String)
    
    case updateFileAuthorizationStatus(id: String, status: SEPatientFileAuthorizedStatus)
}

extension SEChatAPI: TargetType {
    var baseURL: URL { URL(string: SEApiManager.baseUrl)! }
    
    var path: String {
        switch self {
        case .sendGuideInquirySuccessMessage: return "/smarteye/smarteye-inquiry/guidance/sendFirstMessage"
        case .listOfChatRecords: return "/ng/newfuture-msg/imMsg/getSingleHistoryMsg"
        case .generateMessageId: return "/ng/newfuture-msg/im/getMsgId"
        case .updateFileAuthorizationStatus: return "/smarteye/smarteye-inquiry/consultation/updateAuthorizationStatus"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .listOfChatRecords,
             .generateMessageId,
             .updateFileAuthorizationStatus:
            return .post
        case .sendGuideInquirySuccessMessage:
            return .get
        }
    }
    
    var sampleData: Data { "".data(using: .utf8)! }
    
    var task: Task {
        SELog("\(path) - \(parameters)")
        switch self {
        case .sendGuideInquirySuccessMessage:
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case .listOfChatRecords,
             .updateFileAuthorizationStatus:
            return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
        case .generateMessageId:
            return .requestParameters(parameters: parameters, encoding: URLEncoding.httpBody)
        }
    }
    
    var headers: [String : String]? { SEApiManager.headers }
    
    private var parameters: [String: Any] {
        switch self {
        case .sendGuideInquirySuccessMessage(let id):
            return ["id": id]
        case .listOfChatRecords(let relationId, let pageNum, let pageSize):
            return ["relationId": relationId, "isAdmin": 0, "pageNum": pageNum, "pageSize": pageSize]
        case let .generateMessageId(relationId, msgType):
            return ["relationId": relationId, "msgType": msgType]
        case let .updateFileAuthorizationStatus(id, status):
            return ["id": id, "authorizationStatus": status.rawValue]
        }
    }
}
